package com.rcloud.server.sealtalk.service;

import com.rcloud.server.sealtalk.configuration.SealtalkConfig;
import com.rcloud.server.sealtalk.util.AesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 16:07 2019/6/3 2019
 */
@Service
public class SecurityService {

    @Resource
    private SealtalkConfig sealtalkConfig;

    @Autowired
    private RedisService redisService;


    @Transactional(rollbackFor = Exception.class)
    public String token(String salt, Long userId) throws Exception {
        String token = AesUtil.encrypt(salt + ":" + userId, sealtalkConfig.getAesKey());
        redisService.setToken(userId, token);
        return token;
    }


    @Transactional(rollbackFor = Exception.class)
    public String token2(String salt, Long userId) throws Exception {
        long time = System.currentTimeMillis();
        String token = AesUtil.encrypt(salt + ":" + userId + ":" + time, sealtalkConfig.getAesKey());
        redisService.setToken2(userId, time, token);
        return token;
    }


    public Integer validToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        try {
            String de = AesUtil.decrypt(token, sealtalkConfig.getAesKey());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
//                String oldToken = redisService.getToken(userId);
//                if (StringUtils.isNotBlank(oldToken)) {
//                    if (oldToken.equals(token)) {
                        return userId.intValue();
//                    }
//                }
            } else {
                System.out.println(token);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
        }
        return null;
    }


    public Integer validToken2(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        try {
            String de = AesUtil.decrypt(token, sealtalkConfig.getAesKey());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                Long time = NumberUtils.toLong(de.split(":")[2]);
                String oldToken = redisService.getToken2(userId, time);
                if (StringUtils.isNotBlank(oldToken)) {
                    if (oldToken.equals(token)) {
                        return userId.intValue();
                    }
                }
            } else {
                System.out.println(token);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
        }
        return null;
    }


    public void clearToken(Long userId) {
        redisService.deleteToken(userId);
    }


    public void clearToken2(String token) {
        try {
            String de = AesUtil.decrypt(token, sealtalkConfig.getAesKey());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                Long time = NumberUtils.toLong(de.split(":")[2]);
                redisService.deleteToken2(userId, time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean checkToken(String token) {
//        if (StringUtils.isBlank(token)) {
//            return false;
//        }
//        try {
//            String de = AesUtil.decrypt(token, sealtalkConfig.getAesKey());
//            if (StringUtils.isNotBlank(de)) {
//                Long userId = NumberUtils.toLong(de.split(":")[1]);
//                return redisService.valid(userId, token);
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return false;
    }
}
